import * as THREE from 'three'
import { AxesHelper } from 'three';
import { Scene } from 'three';
import { Mesh } from 'three';
import { MeshBasicMaterial } from 'three';
import { WebGLRenderer } from 'three';
import { PerspectiveCamera } from 'three';
import { BoxGeometry } from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import beyond from './public/beyond.png'
import { TextureLoader } from 'three';
const TWEEN = require("@tweenjs/tween.js");
//初始化场景
let scene, camera, rederer, mesh, controls;
function init() {
    scene = new Scene();

}
//透视i相机初始化
function initCamera() {
    camera = new PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 10;
}
//初始化渲染器
function initRenderer() {
    rederer = new WebGLRenderer({
        //棱角
        antialias: true
    });
    rederer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(rederer.domElement);
}
//坐标
function initAxesHelper() {
    const axesHelper = new AxesHelper(3);
    scene.add(axesHelper);
}
//轨道控制器
function initOrbitControls() {
    controls = new OrbitControls(camera, rederer.domElement);
}
//物体
function initMesh() {
    //形状
    const geometry = new BoxGeometry(1, 1, 1)
    //花纹
    const texture = new TextureLoader().load(beyond)
    //材质
    const material = new MeshBasicMaterial({
        color: 'white',
        map: texture
    })
    mesh = new Mesh(geometry, material);
    scene.add(mesh)
}
function initt() {
    init()
    initCamera()
    initRenderer()
    initAxesHelper()
    initOrbitControls()
    initMesh()
    const coords = { x: 0 };
    const tween = new TWEEN.Tween(coords).to({ x: 3 }, 3000).easing(TWEEN.Easing.Quadratic.Out).onUpdate((that) => {
        mesh.position.x = that.x
    }).start()
}
initt()
//旋转动画等
function render(time) {
    //动画
    // if(mesh.position.x>3){

    // }else{
    //     mesh.position.x+=0.01;

    // }

    rederer.render(scene, camera)
    requestAnimationFrame(render)
    TWEEN.update(time)
}
render()
window.addEventListener('resize', function () {
    //camera全局适配
    camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix()
//renderer
rederer.setSize(window.innerWidth,window.innerHeight)
})